package org.noote.libs.camera.preview;

import java.io.IOException;

import org.noote.libs.camera.CameraInterface;

import android.content.Context;
import android.util.Log;
import android.view.SurfaceHolder;
import android.view.SurfaceView;

public class CameraPreview extends SurfaceView implements SurfaceHolder.Callback {
	private static final String TAG = "CameraPreview";

	protected SurfaceHolder _holder = null;
	protected CameraInterface _camera = null;

	public CameraPreview(Context context, CameraInterface camera) {
		super(context);

		// set camera
		_camera = camera;
		
		// Install a SurfaceHolder.Callback so we get notified when the
		// underlying surface is created and destroyed.
		_holder = getHolder();
		_holder.addCallback(this);
		// deprecated setting, but required on Android versions prior to 3.0
		_holder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
	}

	public boolean isInPreview()
	{
		return _camera.isInPreview();
	}
	public boolean startPreview()
	{
		return _camera.startPreview();
	}
	public boolean stopPreview()
	{
		return _camera.stopPreview();
	}
	
	public CameraInterface getCamera() {
		return _camera;
	}

	@Override
	public void surfaceCreated(SurfaceHolder holder) {
		// The Surface has been created, acquire the camera and tell it where
		// to draw.
		if(_camera == null || _camera.getCamera() == null)
		{
			Log.e(TAG, "surfaceCreated() : Camera not active!");
			return;
		}
		try {
			_camera.getCamera().setPreviewDisplay(holder);
			_camera.getCamera().setPreviewCallback(_camera); // force output...
		} catch (IOException e) {
			Log.e(TAG, "Cannot associate holder to camera!");
			e.printStackTrace();			
		}
//		Helper_Camera.setCameraDisplayOrientation(_holder.getSurface(), _camera.getCameraID(), _camera.getCamera());
	}

	@Override
	public void surfaceDestroyed(SurfaceHolder holder) {
		if(_camera == null)
		{
			Log.e(TAG, "surfaceDestroyed() : Camera not active!");
			return;
		}
		
		// Surface will be destroyed when we return, so stop the preview.
		// Because the CameraDevice object is not a shared resource, it's very
		// important to release it when the activity is paused.
		if(_camera.isInPreview())
		{
			_camera.stopPreview();
		}
		if(_camera.getCamera() != null)
		{
			try {
				_camera.getCamera().setPreviewDisplay(null);
			} catch (IOException e) {
				Log.e(TAG, "Cannot associate holder to camera!");
				e.printStackTrace();				
			}
			return;
		}
	}

	@Override
	public void surfaceChanged(SurfaceHolder holder, int format, int w, int h) {
		// Now that the size is known, set up the camera parameters and begin
		// the preview.
		if(_camera == null)
		{
			// no camera ?
			Log.e(TAG, "surfaceChanged() : Camera not active!");
			return;
		}
		
        if (_holder.getSurface() == null){
          // preview surface does not exist
        	Log.e(TAG, "surfaceChanged() : _holder.getSurface() is null");
          return;
        }
        
        if(!_camera.setPreviewSize(w, h))
        {
        	Log.e(TAG, "surfaceChanged() : problem in setBestPreviewSize() ?");
        }
        
        try {
			_camera.getCamera().setPreviewDisplay(holder);
			_camera.getCamera().setPreviewCallback(_camera); // force output...
		} catch (IOException e) {
			Log.e(TAG, "surfaceChanged() : setPreviewDisplay IOException");
			return;
		}        
	}	
}
